package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.util.JQPage;

public class DeviceTypeManagerController extends Controller {
	public void index() {
		this.render("/device_manager/type_manager/deviceType.html");
	}

	public void queryDeviceType() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String code = this.getPara("code");
		String name = this.getPara("name");
		String parent_id = this.getPara("parent_id");
		String model_code = this.getPara("model_code");
		String is_air = this.getPara("is_air");
		//查询用户组织的所有子组织
		
		sb.append("select t.type_id, t.code, t.name, t.model_code,  ");
		sb.append("t.parent_id , tt.name as parent_name, t.is_air  ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from idc_device_type  t left join idc_device_type tt on t.parent_id = tt.type_id where 1=1 ");
		if(code !=null ){
			sqlFrom.append(" and t.code like '%" + code + "%' ");
		}
		
		if(name !=null ){
			sqlFrom.append(" and t.name like '%" + name + "%' ");
		}
		
		if(parent_id !=null ){
			sqlFrom.append(" and t.parent_id = " +  parent_id );
		}
		
		if(model_code !=null ){
			sqlFrom.append(" and t.model_code like '%" +  model_code + "%' ");
		}
		
		if(is_air !=null ){
			sqlFrom.append(" and t.is_air = " +  is_air + " ");
		}
		
		
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}

	public void add() {
		String code = this.getPara("code");
		String name = this.getPara("name");
		String parent_id = this.getPara("parent_id");
		String model_code = this.getPara("model_code");
		String is_air = this.getPara("is_air");
		String sql = "insert into idc_device_type (code, name, parent_id, model_code, is_air) values(?,?,?,?,?)";
		int  result = Db.update(sql, new Object[]{code, name, parent_id, model_code, is_air});
		this.renderJson(result);
	}

	public void delete() {
		final String typeIds = this.getPara("typeIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] typeIdsArr = typeIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_device_type where type_id in(");
				for(int i=0; i<typeIdsArr.length; i++){
					if(i==typeIdsArr.length-1){
						sb.append(typeIdsArr[i] + ")");
					}else{
						sb.append(typeIdsArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==typeIdsArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String typeId = this.getPara("typeId");
		String code = this.getPara("code");
		String name = this.getPara("name");
		String parent_id = this.getPara("parent_id");
		String model_code = this.getPara("model_code");
		String is_air = this.getPara("is_air");
		String sql = "update idc_device_type set code=?, name=?, parent_id=?, model_code=?, is_air=?  where type_id = ? ";
		int  result = Db.update(sql, new Object[]{code, name, parent_id, model_code, is_air, typeId});
		this.renderJson(result);
	}
	
	public void getDeviceTypeList(){
		StringBuffer sb = new StringBuffer();
		sb.append("select type_id, name from idc_device_type ");
		List<Record>  result = Db.find(sb.toString());
		this.renderJson(result);
	}
}
